<?php
namespace app\common\model;

class JobDistribution extends Base
{
    /**
     * 职位分配
     * @param int $userId  操作人ID
     * @param string $staffIds  分配人员ID（逗号隔开的字符串）
     * @param int $jobId  职位ID
     */
    public static function addJobDistribution($userId, $staffIds, $jobId)
    {
        /** 操作人身份 0=管理员，1=主管，2=员工 */
        $userRole = User::getUserRole($userId);
        $type = $userRole ? 2 : 1;

        foreach(explode(',', $staffIds) as $staffId){
            $param[] = [
                'user_id'     => $userId,
                'staff_id'    => $staffId,
                'job_id'      => $jobId,
                'type'        => $type,
                'create_time' => time(),
            ];
        }

        $flag = self::addAll($param);
        if(!$flag) return false;
        return true;
    }

    /**
     * 职位未分配人员列表
     * @param int $userId  操作人ID
     * @param int $jobId  职位ID
     */
    public static function notJobUserList($userId, $jobId)
    {
        $userInfo = User::getFind(['id' => $userId]);

        /** 操作人身份 0=管理员，1=主管，2=员工 */
        $userRole = User::getUserRole($userId, 2);
        $type = $userRole ? 2 : 1;

        /** 已分配人员ID */
        $staffIds = self::getCol(['user_id' => $userId, 'type' => $type, 'job_id' => $jobId], 'staff_id');
        if($staffIds) $where[] = ['id', 'not in', $staffIds];

        /** 所有交付部门的Id */
        $sectionIds = UserSection::getCol(['cate_id' => 2], 'id');

        /** 交付部门主管等级ID */
        $levelIds = UserSectionLevel::getCol([['section_id', 'in', $sectionIds], ['level', '=', 1]], 'id');

        if($userRole != 0){
            $where[] = ['section_id', '=', $userInfo['section_id']];
            $where[] = ['section_level_id', 'not in', $levelIds];
        }else{
            $where[] = ['section_id', 'in', $sectionIds];
            $where[] = ['section_level_id', 'in', $levelIds];
        }
        
        $where[] = ['is_staff', '=', 1];
        $where[] = ['status', '=', 1];

        $list = User::getList($where, 0, 0, 0, 'id,name');
        if(!$list) return [];

        return $list;
    }

    /**
     * 职位参与人列表
     * @param int $userId  操作人ID
     * @param int $jobId  职位ID
     */
    public static function JobPartakeList($userId, $jobId)
    {
        $list = self::getList(['job_id' => $jobId], 0, 0, 0, 'id, user_id, staff_id, type, create_time');
        if(!$list) return [];

        $backData = [];
        foreach($list as $k => &$info){
            $userInfo = User::getFind(['id' => $info['staff_id']], 'id, name, section_id, section_level_id', 'section_id, section_level_id');

            if(!$userInfo){
                unset($list[$k]);
                continue;
            }

            if($userInfo['section_id']){
                $sectionName = UserSection::getVal(['id' => $userInfo['section_id']], 'name');
                if($userInfo['section_level_id']) $sectionName .= '(' . UserSectionLevel::getVal(['id' => $userInfo['section_level_id']], 'name') . ')';
            }else{
                $sectionName = '';
            }

            /** 操作人身份 0=管理员，1=主管，2=员工 */
            $userRole = User::getUserRole($userId);
            $type = $userRole ? 2 : 1;

            if($type == 0){
                $isDis = 1;
            }else if($type == 1 && $info['type'] == 2){
                $isDis = 1;
            }else{
                $isDis = 0;
            }

            $backData[] = [
                'id'           => $info['id'],
                'user_name'    => User::getVal(['id' => $info['user_id']], 'name'),
                'staff_name'   => $userInfo['name'],
                'section_name' => $sectionName,
                'create_time'  => $info['create_time'],
                'is_dis'       => $isDis,
            ];
        }

        return $backData;
    }

    /**
     * 取消职位分配
     * @param int $jobDisId  分配ID
     */
    public static function delJobDistribution($jobDisId)
    {
        $info = self::getFind(['id' => $jobDisId]);
        if(!$info) return false;

        $flag = self::destroy($jobDisId);
        if(!$flag) return false;
        return true;
    }
}